package com.asp.alumnisystem.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.asp.alumnisystem.app.App;


public class UiUtils {

	/**
	 * 通过resId获得字符串数组
	 */
	public static String[] getStringArray(int resId) {
		return getResource().getStringArray(resId);
	}

	/**
	 * 通过资源id获取string文件中的字符串
     */
	public static String getString(int resId) {
		return getResource().getString(resId);
	}

	/**
	 * 根据id获取dimens文件中的数值
     */
	public static float getDimension(int resId) { return getResource().getDimension(resId); }

	/** 通过资源Id获得Drawable对象  */
	public static Drawable getDrawable(int resId) {
		return getResource().getDrawable(resId);
	}

	public static Resources getResource() {
		return getContext().getResources();
	}

	/**
	 * 获得Application对应的Context
	 */
	public static Context getContext() {
		return App.getAppContext();
	}

	/** dip转换px */
	public static int dip2px(int dip) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}

	/** px转换dip */
	public static int px2dip(int px) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

	/** 布局转换器 */
	public static View inflate(int layoutResId) {
		return View.inflate(getContext(), layoutResId, null);
	}

	/**
	 * 跳转到指定Activity,不关闭自身
	 */
	public static<T extends Context> void jumpToPage(T act, Class<?> clazz){
		Intent intent = new Intent(act, clazz);
		act.startActivity(intent);
	}

	/**
	 * 跳转到指定Activity,并关闭自身
	 * @param act
	 * @param clazz
     */
	public static<T extends Activity> void jumpToPageAndFinishSelf(T act, Class<?> clazz){
		jumpToPage(act, clazz);
		act.finish();
	}

	/**
	 * 跳转到指定Activity,不关闭自身
	 */
	public static <T extends Context> void jumpToPage(T act, Class<?> clazz, Bundle bundle) {
		Intent intent = new Intent(act, clazz);
		intent.putExtras(bundle);
		act.startActivity(intent);
	}

	public static View getRootView(Activity context) {
		return ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
	}

	public static int getStatusBarHeight(Activity activity) {
		int height = 0;
		int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen",
				"android");
		if (resourceId > 0) {
			height = activity.getResources().getDimensionPixelSize(resourceId);
		}
		return height;
	}

	public static int getScreenWith() {
		return App.getAppContext().getResources().getDisplayMetrics().widthPixels;
	}

	/**
	 * 隐藏键盘
	 *
	 * @param token hide softInputMethod
	 */
	public static void hideSoftInputMethod(Context context, View token) {
		InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		manager.hideSoftInputFromWindow(token.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 弹出键盘
	 */
	public static void popSoftInputMethod(Context context) {
		new PopKeyboardHandler(context).sendEmptyMessageDelayed(1, 500);
	}

	static class PopKeyboardHandler extends Handler {
		private Context mContext;

		public PopKeyboardHandler(Context context) {
			this.mContext = context;
		}

		@Override
		public void handleMessage(Message msg) {
			if (mContext != null) {
				InputMethodManager manager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				manager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

}
